﻿//
// XN4SL - Xna Framework for Silverlight.
//
using Xn4Sl.Framework;
using System;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;
using System.Collections.Generic;

namespace Xn4Sl.Framework.Graphics
{
    /// <summary>
    /// Performs primitive-based rendering, creates resources, handles system-level
    /// variables, adjusts gamma ramp levels, and creates shaders.
    /// </summary>
    public class GraphicsDevice
    {
        /// <summary>
        /// Canvas Control emulates a Game rendering device.
        /// </summary>
        public Canvas UI;

        /// <summary>
        /// Creates a new instance of GraphicsDevice
        /// </summary>
        /// <param name="ui">The rendering canvas.</param>
        public GraphicsDevice(Canvas ui) 
        {
            this.UI = ui;
            this.UI.RenderTransform = new ScaleTransform() { ScaleX = 1, ScaleY = 1 };
        }

        /// <summary>
        /// Returns the collection of textures that have been assigned to the texture
        /// stages of the device. 
        /// </summary>
        private TextureCollection textures = new TextureCollection();
        public TextureCollection Textures 
        {
            get
            {
                return this.textures;
            }
        }

        /// <summary>
        /// Returns the existing unused text block or creates and returns a new one. 
        /// </summary>
        private List<TextBlock> textStrings = new List<TextBlock>();
        internal TextBlock Text
        {
            get
            {
                foreach (TextBlock item in this.textStrings)
                {
                    if (item.Visibility == System.Windows.Visibility.Collapsed)
                    {
                        return item;
                    }
                }
                TextBlock text = new TextBlock();
                this.textStrings.Add(text);
                this.UI.Children.Add(text);
                text.SetValue(Canvas.ZIndexProperty, this.Textures.ZIndex++);
                return text;
            }
        }

        /// <summary>
        /// Gets or sets a viewport identifying the portion of the render target to receive 
        /// draw calls. 
        /// </summary>
        private Viewport viewport;
        public Viewport Viewport 
        {
            get
            {
                return this.viewport;
            }
            set
            {
                this.viewport = value;
                //
                // Update Canvas location and dimensions.
                //
                this.UI.Width = value.Width;
                this.UI.Height = value.Height;
                this.UI.SetValue(Canvas.LeftProperty, (double)value.X);
                this.UI.SetValue(Canvas.TopProperty, (double)value.Y);
                this.UI.Clip = new RectangleGeometry()
                                    {
                                        Rect = new System.Windows.Rect(value.X,
                                                                       value.Y,
                                                                       value.Width,
                                                                       value.Height)
                                    };
            }
        }

        /// <summary>
        /// Clears the viewport to a specified color.
        /// </summary>
        /// <param name="color"></param>
        public void Clear(Color color) 
        { 
            this.UI.Background = new SolidColorBrush(color.ToColor());
        }
    }
}

